package com.example.demorabbitmqsend.rabbitmq;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MqSender {
    public static final Logger log = LoggerFactory.getLogger(MqSender.class);
    @Autowired
    private RabbitTemplate rabbitTemplate;
    /**
     * 直接把消息发送到hello队列中
     * (这里的hello实际上是routingKey,因为没有交换器，所以直接匹配名字为“hello”的队列)
     */
    @GetMapping("/sendHello")
    public String sendHello(){
        String message = "hello:helloQueue";
        log.info("发送消息到hellod队列："+message);
        rabbitTemplate.convertAndSend("hello",message);
        return "发送成功";
    }

    @GetMapping("/sendUser")
    public String sendUser(@RequestBody User user){
        log.info("发送消息到hellod队列：{}", user);
        rabbitTemplate.convertAndSend("haocai-chuku-cloud","yiyuan.bianMa.000001",user);
        return "发送成功";
    }

}
